package com.jwell.panprovider.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import com.jwell.panprovider.application.ConstValue
import com.jwell.panprovider.contract.TransportPlaneContract
import com.jwell.panprovider.entity.TransportPlaneBean
import com.jwell.panprovider.model.TransportPlaneModel
import com.jwell.panprovider.presenter.TransportPlanePresenter
import com.jwell.panprovider.ui.adapter.TransportPlaneAdapter
import com.jwell.panprovider.ui.custom.DateChooseWheelViewDialog
import kotlinx.android.synthetic.main.activity_transport_plane_search.*
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 运输计划搜索
 */
class TransportPlaneSearchActivity : MVPBaseActivity<TransportPlanePresenter>(), TransportPlaneContract.View {
    private var haveSearch = false
    private var searchData = TransportPlaneModel.RequestData()
    private var listData = ArrayList<TransportPlaneBean>()
    private var adapter = TransportPlaneAdapter(listData)
    override fun createPresenter(): TransportPlanePresenter? {
        return TransportPlanePresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transport_plane_search)
        initToolBar("供料计划", true, null)
        toolbar_right_text_save.text = "搜索"
        toolbar_right_text_save.visibility = View.VISIBLE
        setListener()
        initData()
    }

    private fun setListener() {
        text_time1.setOnClickListener {
            val startDateChooseDialog = DateChooseWheelViewDialog(this@TransportPlaneSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                text_time1.text = time

            })
            startDateChooseDialog.setDateDialogTitle("供应开始时间")
            startDateChooseDialog.setTimePickerGone(true)
            startDateChooseDialog.showDateChooseDialog()
        }
        text_time2.setOnClickListener {
            val startDateChooseDialog = DateChooseWheelViewDialog(this@TransportPlaneSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                text_time2.text = time

            })
            startDateChooseDialog.setDateDialogTitle("供应结束时间")
            startDateChooseDialog.setTimePickerGone(true)
            startDateChooseDialog.showDateChooseDialog()
        }
        toolbar_right_text_save.setOnClickListener {
            when (toolbar_right_text_save.text) {
                "重新搜索" -> {
                    list_layout.visibility = View.GONE
                    scrollView_search.visibility = View.VISIBLE
                    haveSearch = false
                }
                "搜索" -> {
                    searchData.filter = edit_filter.text.toString()
                    searchData.fuelStateDate = text_time1.text.toString()
                    searchData.fuelEndDate = text_time2.text.toString()
                    searchData.skipCount = 0
                    searchData.fuelState = 4
                    showProgress("", true)
                    presenter?.getListData(searchData)
                    haveSearch = true
                    list_layout.visibility = View.VISIBLE
                    scrollView_search.visibility = View.GONE
                }
            }
            when (haveSearch) {
                true -> {
                    toolbar_right_text_save.text = "重新搜索"
                }
                false -> {
                    toolbar_right_text_save.text = "搜索"
                }
            }
        }
    }

    private fun initData() {
        initEmptyAndErrorView()
        loadingLayout.visibility = View.GONE
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "未搜索到符合条件的计划"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    override fun showListData(data: List<TransportPlaneBean>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < ConstValue.MAXVUALUE) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }
}
